package cn.edu.xjtu.android.emoji_warfare_android.music;

import java.util.Random;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.SoundPool;
import cn.edu.xjtu.android.emoji_warfare_android.App;
import cn.edu.xjtu.android.emoji_warfare_android.R;

/**
 * 音乐及音效
 * 
 * @author wyyf
 *
 */
public class Musics {
    MediaPlayer bgm;
    AssetManager am;
    SoundPool sp;

    OnCompletionListener loop;

    int lastIndex;

    @SuppressWarnings("deprecation")
    public Musics(Context ct) {

        sp = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
        sp.load(App.instance, R.raw.boom, 1);
        loop = new OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer arg0) {
                nextBGM();
            }
        };
        try {
            am = ct.getAssets();
            AssetFileDescriptor afd = am.openFd(bgmName[getRandomBgmIndex()]);
            bgm = new MediaPlayer();
            bgm.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
            bgm.setOnCompletionListener(loop);
            bgm.prepare();
            bgm.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        instance = this;
    }

    public void nextBGM() {
        if (bgm != null) {
            bgm.stop();
            bgm.release();
        }
        new Thread() {
            public void run() {

                try {
                    AssetFileDescriptor afd = am.openFd(bgmName[getRandomBgmIndex()]);
                    bgm = new MediaPlayer();
                    bgm.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
                    bgm.setOnCompletionListener(loop);
                    bgm.prepare();
                    bgm.start();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    /**
     * 获取随机一首bgm的编号，和上一首不同
     * 
     * @return bgm编号
     */
    private int getRandomBgmIndex() {
        int index;
        while (lastIndex == (index = mRandom.nextInt(bgmName.length)))
            ;
        lastIndex = index;
        return index;
    }

    public void start() {
        try {
            bgm.start();
        } catch (NullPointerException e) {
            // e.printStackTrace();
        }
    }

    public void pause() {
        try {
            bgm.pause();
        } catch (NullPointerException e) {
            // e.printStackTrace();
        }
    }

    final static String[] bgmName = { "relax_1.mp3", "relax_2.mp3", "relax_3.mp3", "relax_4.mp3", "relax_5.mp3",
            "relax_6.mp3", "relax_7.mp3", "excited_1.mp3", "excited_2.mp3", "high_1.mp3", "high_2.mp3", "high_3.mp3",
            "laugh_1.mp3", "base.mp3", "key.mp3" };

    public void boom() {
        sp.play(1, 1, 1, 0, 0, 1.5f);
    }

    private static Musics instance;

    public static Musics getInstance() {
        return instance;
    }

    private static Random mRandom;

    static {
        mRandom = new Random();
    }
}
